<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
  <head>
    <title>推广策略列表</title>
    <script type="text/javascript">
      var newPromotionWin;
      var editPromotionWin;
      var promotionId;
      Ext.onReady(function(){
        //推广策略设置---------------------------------------------------------
        //生成有关推广策略的Grid
        var promotionDS = new Ext.data.Store({
          proxy: new Ext.data.HttpProxy({
            url: '/promotion/getAllPromotions'
          }),
          reader: new Ext.data.JsonReader({
            totalProperty: 'totalProperty',
            root: 'root'
          }, [{
              name: 'id'
            }, {
              name: 'name'
            }, {
              name: 'begin_date'
            }, {
              name: 'end_date'
            }, {
              name: 'employee_name',
              mapping: 'employee.name'
            }, {
              name: 'description'
            }, {
              name: 'created_at'
            }, {
              name: 'updated_at'
            }])
        });
        var promotionSM = new Ext.grid.CheckboxSelectionModel();
        var promotionCM = new Ext.grid.ColumnModel({defaults: {
            sortable: true,
            menuDisabled: false
          },columns:[new Ext.grid.RowNumberer(),promotionSM,{
              header: 'id',
              dataIndex: 'id',
              width: 40
            }, {
              header: '名称',
              dataIndex: 'name',
              width:260
            },{
              header: '开始日期',
              dataIndex: 'begin_date',
              renderer: Ext.util.Format.dateRenderer('Y-m-d'),
              width: 80
            },{
              header: '结束日期',
              dataIndex: 'end_date',
              renderer: Ext.util.Format.dateRenderer('Y-m-d'),
              width: 80
            }, {
              header: '操作员',
              dataIndex: 'employee_name',
              width:90
            }, {
              header: '描述',
              dataIndex: 'description',
              width:330
            },{
              header: '生成日期',
              dataIndex: 'created_at',
              renderer: Ext.util.Format.dateRenderer('Y-m-d H:i'),
              width: 100
            },{
              header: '修改日期',
              dataIndex: 'updated_at',
              renderer: Ext.util.Format.dateRenderer('Y-m-d H:i'),
              width: 100
            }]});
        var promotionGrid =  new Ext.grid.GridPanel({
          ds: promotionDS,
          cm: promotionCM,
          sm: new Ext.grid.RowSelectionModel({singleSelect:true}),
          width: screenWidth-330,
          height: screenHeight-380,
          iconCls: 'icon-grid',
          loadMask: true,
          //collapsible: true,
          //animCollapse: false,
          //frame: true,
          //title: '推广策略列表',
          bbar: new Ext.PagingToolbar({
            pageSize: 10,
            store: promotionDS,
            displayInfo: true,
            displayMsg: "显示第 {0} 条到 {1} 条记录，一共 {2} 条",
            emptyMsg: "没有记录"
          }),
          tbar: [{
              text: '添加',
              iconCls: 'addItem',
              handler : function(){
                newPromotionWin = new Ext.Window({
                  layout: 'fit',
                  width:618,
                  height:300,
                  closeAction: 'destroy',
                  plain: true,
                  modal: true,
                  constrain:true,
                  //modal: true,
                  title: '添加推广策略',
                  autoLoad: {
                    url: "/promotion/newPromotion",
                    scripts: true
                  }
                });
                newPromotionWin.on("destroy",function(){
                  promotionDS.reload();
                });
                newPromotionWin.resizable = false;
                newPromotionWin.show();
              }
            },"-",{
              text: '编辑',
              iconCls: 'editItem',
              handler : function(){
                var records = promotionGrid.getSelectionModel().getSelections();
                if(records.length==0){
                  Ext.MessageBox.show({
                    title: '提示信息',
                    msg: "请先选中一条记录后，再编辑。",
                    buttons: Ext.MessageBox.OK,
                    icon: Ext.MessageBox.WARNING
                  });
                }else{
                  //把表单添加到窗口中
                  promotionId = records[0].get('id');
                  editPromotionWin = new Ext.Window({
                    title: '编辑推广策略',
                    layout:'fit',
                    width:618,
                    height:300,
                    closeAction:'destroy',
                    constrain:true,
                    plain: true,
                    modal: true,
                    autoLoad: {
                      url: "/promotion/editPromotion?id=" + promotionId,
                      scripts: true
                    }
                  });
                }
                editPromotionWin.on("destroy",function(){
                  promotionDS.reload();
                });
                editPromotionWin.resizable = false;
                editPromotionWin.show();
              }
            }]
        });
        promotionGrid.render('promotion_list');
        promotionDS.load();
      })
    </script>
  </head>
  <body>
    <div id="promotion_list"></div>
  </body>
</html>
